package com.ybkj.daijia.config;

import com.ybkj.daijia.Setting;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;

/**
 * @author Blazk.
 */
@Component
public class RentApi {

    public static final String INSERT_NOTICE = "api/rentnotice/insertNotice";
    public static final String DELETE_NOTICE = "api/rentnotice/deleteNotice";
    public static final String UPDATE_NOTICE = "api/rentnotice/updateNotice";
    public static final String SELECT_NOTICE = "api/rentnotice/selectNotice";
    public static final String SELECT_NOTICE_BY_TITLE = "api/rentnotice/selectNoticesByTitle";
    public static final String SELECT_CATEGORY_BY_PID = "api/rentnotice/selectCategoryByPid";
    public static final String INSERT_CATEGORY = "api/rentnotice/insertCategory";
    public static final String DELETE_CATEGORY = "api/rentnotice/deleteCategory";
    public static final String SELECT_CATEGORY = "api/rentnotice/selectCategory";
    public static final String UPDATE_CATEGORY = "api/rentnotice/updateCategory";
    public static final String SELECT_NOTICE_CATEGORY_BY_CATEGORY_NAME = "api/rentnotice/selectNoticeCategorysByCategoryName";
    public static final String INSERT_CATEGORY_TOP = "api/rentnotice/insertCategoryTop";
    public static final String DELETE_CATEGORY_TOP = "api/rentnotice/deleteCategoryTop";
    public static final String UPDATE_CATEGORY_TOP = "api/rentnotice/updateCategoryTop";
    public static final String SELECT_CATEGORY_TOP = "api/rentnotice/selectCategoryTop";
    public static final String SELECT_ALL_CATEGORY_TOP = "api/rentnotice/selectAllCategoryTop";
    public static final String SELECT_NOTICE_CATEGORY_TOP_BY_CATEGORY_NAME = "api/rentnotice/selectNoticeCategoryTopsByCategoryName";
    public static final String INSERT_CONTENT = "api/rentcontent/insertContent";
    public static final String DELETE_CONTENT = "api/rentcontent/deleteContent";
    public static final String SELECT_ALL_PROPERTY = "api/rentcontent/selectAllProperty";
    public static final String SELECT_CONTENT_BY_PID = "api/rentcontent/selectContentByPid";
    public static final String UPDATE_PROPERTY = "api/rentcontent/updateProperty";
    //information的
    public static final String INSERT_INFOMATION = "api/information/insertInforMation";
    public static final String SELECT_INFORMATION_BY_ID = "api/information/selectById";
    public static final String UPDATE_INFOMATION = "api/information/updateInforMation";
    public static final String SELECT_ALL_INFOMATION = "api/information/selectInforMation";
    public static final String BATCH_PASS = "api/information/batchpass";
    //rentshop
    public static final String INSERT_RENTSHOP = "api/rentshop/insertRentshop";
    public static final String DELETE_RENTSHOP = "api/rentshop/deleteRentshop";
    public static final String UPDATE_RENTSHOP = "api/rentshop/updateRentshop";
    public static final String SELECT_RENTSHOP = "api/rentshop/selectRentshop";
    public static final String SELECT_RENTSHOP_BY_SHOP_NAME = "api/rentshop/selectRentshopByShopName";
    //rentorder
    public static final String CONFIRM_RENTORDER = "api/rentorder/confirm";
    public static final String PAYED_RENTORDER = "api/rentorder/payed";
    public static final String CANCEL_RENTORDER = "api/rentorder/cancel";
    public static final String DISPATCH_RENTORDER = "api/rentorder/dispatch";
    public static final String DEPOSIT_RENTORDER = "api/rentorder/deposit";
    public static final String SERVER_RENTORDER = "api/rentorder/server";
    public static final String APPLYRERENT_RENTORDER = "api/rentorder/applyReRent";
    public static final String BACK_RENTORDER = "api/rentorder/back";
    public static final String RATE_RENTORDER = "api/rentorder/rate";
    public static final String OVER_RENTORDER = "api/rentorder/over";
    public static final String SELECT_RENTORDER = "api/rentorder/selectRentOrder";
    public static final String SELECT_RENTORDERS = "api/rentorder/selectRentOrders";
    public static final String SELECT_RENTORDERSCOUNT = "api/rentorder/selectRentOrdersCount";
    public static final String SELECT_RENTORDERSTATUS = "api/rentorder/selectRentOrderStatus";
    public static final String ADD_PECCANCY = "api/rentorder/addPeccancy";
    public static final String SELECT_PECCANCYS_BYORDERID = "api/rentorder/selectPeccancysByOrderId";
    public static final String SELECT_PECCANCYS_BYPASSENGERID = "api/rentorder/selectPeccancysByPassengerId";
    public static final String DISPATCH_CAR = "api/rentorder/dispatchCar";
    public static final String BACKFOREGIFT = "api/rentorder/backForegift";
    public static final String EXPORT_RENTSHOP = "api/rentshop/exportRentShop";
    public static final String MEMO_RENTSHOP = "api/rentorder/memo";
    public static final String POST_PECCANCY = "api/rentorder/postPeccancy";
    //车系接口
    public static final String INSERT_VEHICLE = "api/rentvehicle/insertVehicle";
    public static final String UPDATE_VEHICLE = "api/rentvehicle/updateVehicle";
    public static final String GROUNDING_VEHICLE = "api/rentvehicle/grouding";
    public static final String SELECT_VEHICLE = "api/rentvehicle/findVehicle";
    public static final String SELECT_VEHICLE_BYPARAM = "api/rentvehicle/findVehicleByparams";
    public static final String SELECT_VEHICLE_BYID = "api/rentvehicle/findById";
    public static final String DREDGE_SHOP_VEHICLE = "api/rentshop/dredgeShopVehicle";
    public static final String CLOSE_SHOP_VEHICLE = "api/rentshop/closeShopVehicle";
    public static final String SELECT_SHOP_BY_VEHICLEID = "api/rentshop/findShopVehicleByVehicleId";
    public static final String DELETE_VEHICLEID_HOLIDAY = "api/rentvehicle/deleteHolidayById";
    //车辆接口
    public static final String SELECT_CAR_BYID = "api/car/findById";
    public static final String INSERT_CAR = "api/car/insertCar";
    public static final String UPDATE_CAR = "api/car/updateCar";
    public static final String GROUNDING_CAR = "api/car/updateGrounding";
    public static final String UPDATE_CAR_STATE = "api/car/updateCarStatus";
    public static final String SELECT_CAR = "api/car/findCar";
    public static final String SELECT_CAR_BRANDIDANDPALTENO = "api/car/findByBrandIdAndPlateNo";
    public static final String SELECT_CAR_VEHICLEID = "api/car/findByshopIdAndVehicleId";
    //保险模块 4.13
    public static final String INSERT_RENTINSURANCE = "api/rentinsurance/insertRentInsurance";
    public static final String FIND_RENTINSURANCE_BY_ID = "api/rentinsurance/findRentInsuranceById";
    public static final String DELETE_RENTINSURANCE_BY_CARID = "api/rentinsurance/deleteRentInsuranceByCarId";
    public static final String UPDATE_RENTINSURANCE = "api/rentinsurance/updateRentInsurance";
    public static final String FIND_RENTINSURANCE_BY_CARID = "api/rentinsurance/findRentInsuranceByCarId";
    //APP设置
    public static final String SELECT_RENT_APP_SETTING = "api/rentappsetting/selectByRentAppSettingId";
    public static final String UPDATE_RENT_APP_SETTING = "api/rentappsetting/updateRentAppSetting";
    private static final Logger logger = LoggerFactory.getLogger(RentApi.class);
    @Value("${module.api.url}")
    private String apiBaseURL;
    @Autowired
    private RestTemplate restTemplate;

    public String getApiBaseURL() {

        return apiBaseURL.endsWith("/") ? apiBaseURL : apiBaseURL + "/";
    }

    public void setApiBaseURL(String apiBaseURL) {
        this.apiBaseURL = apiBaseURL;
    }

    public RestTemplate getRestTemplate() {
        return restTemplate;
    }

    public void setRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public String get(String apiurl, Map<String, String> kv, Setting setting) {
        kv.put("appkey", setting.getSecKey());

        String result = null;
        try {
            result = restTemplate.getForObject(apiurl, String.class, kv);
        } catch (HttpClientErrorException e) {
            e.printStackTrace();
            logger.error("HttpClientErrorException:code:{},status:{},body:{}", e.getStatusCode(),
                e.getStatusText(), e.getResponseBodyAsString());
            result = e.getResponseBodyAsString();
        } catch (HttpServerErrorException e) {
            e.printStackTrace();
            logger.error("HttpServerErrorException:{}", e.getResponseBodyAsString());
            result = e.getResponseBodyAsString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;

    }

    public String post(String apiurl, Map<String, String> kv, Setting setting) {

        kv.put("appkey", setting.getSecKey());

        String result = null;
        try {
            result = restTemplate.postForObject(apiurl, null, String.class, kv);
        } catch (HttpClientErrorException e) {
            logger.error("HttpClientErrorException:code:{},status:{},body:{}", e.getStatusCode(),
                e.getStatusText(), e.getResponseBodyAsString());
            result = e.getResponseBodyAsString();
        } catch (HttpServerErrorException e) {
            logger.error("HttpServerErrorException:{}", e.getResponseBodyAsString());
            result = e.getResponseBodyAsString();
        }

        return result;
    }

    public String postMultiValueMap(String apiurl, MultiValueMap<String, String> kv,
        Setting setting) {

        kv.add("appkey", setting.getSecKey());

        Set<Entry<String, List<String>>> entries = kv.entrySet();

        Map<String, String> params = new HashMap<String, String>();
        for (Entry<String, List<String>> entry : entries) {
            List<String> values = entry.getValue();
            params.put(entry.getKey(), CollectionUtils.isEmpty(values) ? null : values.get(0));
        }

        RestTemplate rest = new RestTemplate();

        String result = null;
        try {
            result = rest.postForObject(apiurl, kv, String.class);
        } catch (HttpClientErrorException e) {
            e.printStackTrace();
            logger.error("HttpClientErrorException:code:{},status:{},body:{}", e.getStatusCode(),
                e.getStatusText(), e.getResponseBodyAsString());
            result = e.getResponseBodyAsString();
        } catch (HttpServerErrorException e) {
            e.printStackTrace();
            logger.error("HttpServerErrorException:{}", e.getResponseBodyAsString());
            result = e.getResponseBodyAsString();
        }

        return result;
    }

    public String put(String apiurl, Map<String, String> kv, Setting setting) {
        kv.put("appkey", setting.getSecKey());

        String result = null;
        try {
            restTemplate.put(apiurl, null, kv);
        } catch (HttpClientErrorException e) {
            e.printStackTrace();
            result = e.getResponseBodyAsString();
        } catch (HttpServerErrorException e) {
            e.printStackTrace();
            logger.error("HttpServerErrorException:{}", e.getResponseBodyAsString());
            result = e.getResponseBodyAsString();
        }
        return result;

    }

    public String delete(String apiurl, Map<String, String> kv, Setting setting) {
        kv.put("appkey", setting.getSecKey());

        String result = null;

        try {
            restTemplate.delete(apiurl, kv);
        } catch (HttpClientErrorException e) {
            result = e.getResponseBodyAsString();
        } catch (HttpServerErrorException e) {
            logger.error("HttpServerErrorException:{}", e.getResponseBodyAsString());
            result = e.getResponseBodyAsString();
        }
        return result;
    }


}
